import React from 'react';
import {Table, Card} from 'antd';
import PropTypes from 'prop-types';
import {formatAmount, formatDateTime, conventEnumValueToString} from '../utils';
import {TABLE, PAGINATION_OPTIONS, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATE_FORMAT} from '../constants';
import {partClaimType, currency, partClaimReasonStatus, partClaimFormStatus} from '../Enum';
export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const {formatMessage} = this.props.intl;
        const {pageIndex, pageSize, total} = this.props;
        const locale = {
            emptyText: this.props.hasError ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const columns = [{
            title: formatMessage({
                id: 'tablePanel.column.order',
                defaultMessage: '序号'
            }),
            dataIndex: 'order'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.code',
                defaultMessage: '备件索赔单编号'
            }),
            dataIndex: 'code',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'code' && this.props.sortedOrder
        }, {
            title: formatMessage({
                id: 'tablePanel.column.submitTime',
                defaultMessage: '提报时间'
            }),
            dataIndex: 'submitTime',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'submitTime' && this.props.sortedOrder,
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.shipmentDate',
                defaultMessage: '发货日期'
            }),
            dataIndex: 'shipmentDate',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'shipmentDate' && this.props.sortedOrder,
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.claimType',
                defaultMessage: '索赔类型'
            }),
            dataIndex: 'claimType',
            render: text => conventEnumValueToString(partClaimType, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.dealerCode',
                defaultMessage: '总代号'
            }),
            dataIndex: 'dealerCode'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.invoiceCode',
                defaultMessage: '发票号'
            }),
            dataIndex: 'invoiceCode'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partCode',
                defaultMessage: '零件号'
            }),
            dataIndex: 'partCode'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partName',
                defaultMessage: '零件名称'
            }),
            dataIndex: 'partName'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.claimAmount',
                defaultMessage: '数量'
            }),
            dataIndex: 'claimAmount'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.currency',
                defaultMessage: '币种'
            }),
            dataIndex: 'currency',
            render: text => conventEnumValueToString(currency, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.price',
                defaultMessage: '单价'
            }),
            dataIndex: 'price',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.sum',
                defaultMessage: '合计'
            }),
            dataIndex: 'sum',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.sumWithRate',
                defaultMessage: '合计2'
            }),
            dataIndex: 'sumWithRate',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.claimReason',
                defaultMessage: '索赔原因'
            }),
            dataIndex: 'claimReason',
            render: text => conventEnumValueToString(partClaimReasonStatus, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.payAmount',
                defaultMessage: '赔付金额'
            }),
            dataIndex: 'payAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.cacuFee',
                defaultMessage: '实际赔付金额'
            }),
            dataIndex: 'cacuFee',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.confirmTime',
                defaultMessage: '完成赔付时间'
            }),
            dataIndex: 'confirmTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.status',
                defaultMessage: '状态'
            }),
            dataIndex: 'status',
            render: text => conventEnumValueToString(partClaimFormStatus, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.remark',
                defaultMessage: '备注'
            }),
            dataIndex: 'remark'
        }];
        return (
            <Card>
                <Table
                    className={'white-space-nowrap'}
                    columns={columns}
                    dataSource={this.props.data}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    pagination={pagination}
                    rowKey="order"
                    locale={locale}
                    {...TABLE}/>
            </Card>
        );
    }
}
TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    hasError: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';
import {injectIntl} from './intl';

const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    hasError: state.getIn(['page', 'domainData', 'list', 'hasError']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    isDealerUser: state.getIn(['page', 'domainData', 'init', 'isDealerUser']),
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
